package pl.edu.uj.ii.battleships.activities;

import pl.edu.uj.ii.battleships.R;
import pl.edu.uj.ii.battleships.utils.GameDataManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class HostActivity extends CustomActivity {
	
	//TextView gameName;
	TextView nick;
	Spinner boardHeigth;
	Spinner boardWidth;
	Spinner fivemast;
	Spinner fourmast;
	Spinner threemast;
	Spinner twomast;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.host_layout);
        
        //gameName = (TextView)findViewById(R.id.textview_gamename);
        nick = (TextView)findViewById(R.id.textview_nick);
        
        boardHeigth = (Spinner)findViewById(R.id.spinner_boardHeigth);
        boardWidth = (Spinner)findViewById(R.id.spinner_boardWidth);
        twomast = (Spinner)findViewById(R.id.spinner_2mast);
        threemast = (Spinner)findViewById(R.id.spinner_3mast);
        fourmast = (Spinner)findViewById(R.id.spinner_4mast);
        fivemast = (Spinner)findViewById(R.id.spinner_5mast);
        
        Button creategame = (Button)findViewById(R.id.button_creategame);
        creategame.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				//GameDataManager.setGameName(gameName.getText().toString());
				
				if(nick.getText().toString().equals("") || nick == null) {
					Toast t = Toast.makeText(getApplicationContext(), "Enter your nickname", Toast.LENGTH_SHORT);
					t.show();
					return;
				} else {
					GameDataManager.setMyNick(nick.getText().toString());
				}
				
				GameDataManager.setBoardHeight(Integer.parseInt((String)boardHeigth.getSelectedItem()));
				GameDataManager.setBoardWidth(Integer.parseInt((String)boardWidth.getSelectedItem()));
				
				int twm = Integer.parseInt((String)twomast.getSelectedItem());
				int thm = Integer.parseInt((String)threemast.getSelectedItem());
				int fom = Integer.parseInt((String)fourmast.getSelectedItem());
				int fim = Integer.parseInt((String)fivemast.getSelectedItem());
				
				if (twm > 0 || thm > 0 || fom > 0 || fim > 0) {
					GameDataManager.setTwomast(twm);
					GameDataManager.setThreemast(thm);
					GameDataManager.setFourmast(fom);
					GameDataManager.setFivemast(fim);
					GameDataManager.setIsServer(true);
					
					Intent intent = new Intent(v.getContext(), MyBoardActivity.class);
					startActivityForResult(intent, 0); 
				} else {
					Toast t = Toast.makeText(getApplicationContext(), "You must choose at least one ship", Toast.LENGTH_SHORT);
					t.show();
				}
			}
		});
	}
}
